import Vue from 'vue'
import App from './App.vue'
import router from './router'
import 'element-ui/lib/theme-chalk/index.css';
import ElementUI from 'element-ui';
import { postRequest } from './utils/api';
import { getRequest } from './utils/api';
import { putRequest } from './utils/api';
import { deleteRequest } from './utils/api';
import {downloadRequest} from './utils/download'
Vue.config.productionTip = false
Vue.use(ElementUI, { size: 'small' });
Vue.prototype.postRequest = postRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.deleteRequest = deleteRequest;
Vue.prototype.downloadRequest = downloadRequest;
import store  from './store';
import { initMenu } from './utils/menus';
import 'font-awesome/css/font-awesome.css'



router.beforeEach((to, from, next) => { 
  // console.log(to);
  // console.log(from);
  // console.log(next);
  if (window.sessionStorage.getItem('token')) {
    initMenu(router, store);
    if (!window.sessionStorage.getItem('user')) {
      getRequest('/admin/info').then((resp) => { 
      //解决sessionStorage装载慢的问题
        store.commit('userValue',JSON.parse(JSON.stringify(resp)));
        
      })
    }
    next();
  } else {
    if (to.path == '/') {
      next();
    } else { 
      next('/?redirect='+to.path);
    }
      
    
   
  }
 
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
